<?php
namespace app\admin\controller;


use app\BaseController;
use app\admin\middleware\CheckLogin;
use think\App;
use think\facade\Session;
use think\facade\View;
use think\Request;

class Base extends BaseController
{
    // 是否自动实例化当前模型
    protected $autoNewModel = true;

    // 自定义实例化模型位置
    protected $ModelPath = null;

    // 实例化当前模型
    protected $M = null;

    // 是否开启自动验证
    protected $autoValidateCheck = true;

    // 不需要验证的方法
    protected $excludeValidateCheck = [];

    // 验证场景配置
    protected $autoValidateScenes = [];

    // 当前控制器信息
    protected $Cinfo = [];

    protected $admin = null;
    protected $pageSize = 0;

    // 在profile方法上使用中间件
    protected $middleware = [
        CheckLogin::class
    ];

    public function __construct(Request $request)
    {
//        echo 22;
        if(Session::get('health_admin'))
        {
            $this->admin = Session::get('health_admin');
//            dd(1);
            //        dd(($this->admin)['id']);
            $this->pageSize = 10;
//            dd(Session::get('health_admin'));
//        if (Session::has('merchant')) {
//            dd(Session::get('merchant'));
////            return $next($request);
//        } else {
//            return redirect(url('merchant/login/index'));
//        }
            if($this->admin && ($this->admin)['id'] != 1){
                // 初始化控制器相关信息
                $this->initControllerInfo($request);
//        dd($this->Cinfo);
                $str = ($this->Cinfo)['Cname'] . '/' . ($this->Cinfo)['action'];
                //dd($str);
                switch ($str){
                    case 'User/recharge':
                    case 'User/deduct':
                    case 'User/changeLimitBenefit':
                        {
//                dd(12);
                            $arr['status'] = 0;
                            $arr['msg'] = '暂无权限';
//                        return json($arr);
//                echo $arr;
                            return ApiException('暂无权限');
//                        return showError('暂无权限');
                        }
                        break;
                    case 'UserWithdraw/withdraw':
                    {
                        if(($this->admin)['id'] != 2 && ($this->admin)['id'] != 3){
                            $arr['status'] = 0;
                            $arr['msg'] = '暂无权限';
                            return ApiException('暂无权限');
                        }
                    }
                }
            }

            View::assign(array(
                'admin_name'        =>    ($this->admin)['username'],
                'groupid'        =>    ($this->admin)['groupid'],
            ));
//        // 实例化当前模型
//        $this->getCurrentModel();
        }else{
//            dd(2);
//            return redirect(url('admin/login/index'));
//            dd(Session::get('*'));
//            dd(2);
        }

    }

    // 初始化控制器相关信息
    public function initControllerInfo($request)
    {
        $str = $request->controller();
        // 获取真实控制器名称
        //  $arr = explode('.',$str);
        //  $Cname = $arr[count($arr)-1];
        $this->Cinfo = [
            'Cname' => class_basename($this),
            'Cpath' => str_replace('.', '\\', $str),
            // 当前方法
            'action' => $request->action()
        ];
    }

    // 获取当前模型实例
    public function getCurrentModel()
    {
        if (!$this->M && $this->autoNewModel) {
            $model = $this->ModelPath ? $this->ModelPath : $this->Cinfo['Cpath'];
            $this->M = app('app\\admin\\model\\' . $model);
        }
    }
}